package server

import (
	"360-cinema/user-server/basic/handler/model"
	"360-cinema/user-server/basic/inits"
	__ "360-cinema/user-server/basic/proto"
	"context"
)

type Server struct {
	__.UnimplementedUserServer
}

func (s Server) RegisterUser(_ context.Context, in *__.RegisterUserReq) (*__.RegisterUserResp, error) {
	//TODO implement me

	var user model.User
	user = model.User{
		Phone:    in.Phone,
		Password: in.Password,
		Username: in.Username,
		Avatar:   in.Avatar,
		Status:   in.Status,
	}
	result, err := inits.Rdb.Get(inits.Ctx, "sendSms").Result()
	if err != nil {
		return nil, err
	}
	if in.Code != result {
		return &__.RegisterUserResp{
			Code: 0,
			Msg:  "验证码错误",
		}, nil
	}
	err = model.RegisterUser(&user)
	if err != nil {
		return &__.RegisterUserResp{
			Code: 0,
			Msg:  "注册失败",
		}, nil
	}
	return &__.RegisterUserResp{
		Code: 200,
		Msg:  "注册成功",
		Data: int64(user.ID),
	}, nil

}
